// 客服列表数据

import { createSlice } from "@reduxjs/toolkit";

export const customerServiceList = createSlice({
  name: "customerServiceList",
  initialState: {
    csList: JSON.parse(localStorage.getItem("chijiuhua")) || [],
  },
  reducers: {
    csListSet: (state, action) => {
      state.csList = action.payload;
      // console.log(action.payload);
      localStorage.setItem("chijiuhua", JSON.stringify(state.csList));
    },
    // 搜索
    searchSet: (state, action) => {
      state.csList = state.csList.filter((item, index) => {
        return item.name.includes(action.payload);
      });
    },
    // 删除
    delSet: (state, action) => {
      const indexItem = state.csList.findIndex(
        (item) => item.key === action.payload.key
      );
      state.csList.splice(indexItem, 1);
    },
    // 编辑
    editSet: (state, action) => {
      console.log(action.payload);
      let newState = [...state.csList];
      let indexItem = newState.findIndex(
        (item) => item.key === action.payload.key
      );
      let obj = {
        key: indexItem + 1,
        ...action.payload,
      };
      newState.splice(indexItem, 1, obj);
      console.log(typeof obj.key);
      state.csList = newState;
    },
    // 修改密码
    modifySet: (state, action) => {
      console.log(action.payload);
      let newState = [...state.csList];
      let indexItem = newState.findIndex(
        (item) => item.key === action.payload.key
      );
      newState.splice(indexItem, 1, action.payload);
      state.csList = newState;
    },

    // 添加
    addcustomer: (state, action) => {
      state.csList.unshift(action.payload);
      console.log(action.payload);
    },
  },
});
// 每个 case reducer 函数会生成对应的 Action creators
export const { csListSet, searchSet, delSet, editSet, addcustomer, modifySet } =
  customerServiceList.actions;

export default customerServiceList.reducer;
